<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{number}}.{{title}}</title>
    <!-- 引⼊ACE插件 --> 
    <!-- 官⽹链接：https://ace.c9.io/ --> 
    <!-- CDN链接：https://cdnjs.com/libraries/ace --> 
    <!-- 使⽤介绍：https://www.iteye.com/blog/ybc77107-2296261 --> 
    <!-- https://justcode.ikeepstudying.com/2016/05/ace-editor-%E5%9C%A8%E7%BA%BF%E4%BB%A3%E7%A0%81%E7%BC%96%E8%BE%91%E6%9E%81%E5%85%B6%E9%AB%98%E4%BA%AE/ -->
    <!-- 引入ACE CDN -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript"charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/extlanguage_tools.js"type="text/javascript"charset="utf-8"></script>
    <!-- 引⼊jquery CDN --> 
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        html,
        body {
            width: 100%;
            height: 100%;
        }
        .container .navbar{
            width: 100%;
            height: 50px;
            background-color: black;
            /* 给父级标签设置overflow,取消后续float带来的影响 */
            overflow: hidden;
        }
        .container .navbar a{
            /* 设置a标签是行内块元素 */
            display: inline-block;
            /* 设置a标签的宽度 */
            width: 100px;
            /* 设置字体颜色和大小 */
            color: white;
            font-size: larger;
            /* 设置文字高度 */
            line-height: 50px;
            /* 去掉a标签的下划线 */
            text-decoration: none;
            /* 设置a标签的文字居中 */
            text-align: center;
        }
        /*设置鼠标事件*/
        .container .navbar a:hover{
            background-color: green;
        }
        .container .navbar .login{
            float: right;
        }
        .container .navbar .Register{
            float: right;
        }
        .container .part1{
            width:100%;
            height: 600px;
            overflow: hidden;
        }
        .container .part1 .left_desc{
            width: 50%;
            height: 600px;
            float: left;
            overflow: scroll;
        }
        .container .part1 .left_desc h4{
            padding-top: 10px;
            padding-left: 10px;
        }
        .container .part1 .left_desc pre{
            padding-top: 10px;
            padding-left: 10px;
            font-size: medium;
            font-family:'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
        }
        .container .part1 .right_code{
            width: 50%;
            float: right;
        }
        .container .part1 .right_code .ace_editor{
            height: 600px;
        }
        .container .part2{
            width:100%;
            overflow: hidden;
        }
        .container .part2 .result{
            width:300px;
            float:left;
        }
        .container .part2 .btn-submit{
            width:120px;
            height:50px;
            font-size: large;
            float: right;
            background-color: #26bb9c;
            color:#FFF;
            /* 给按钮带圆角 */
            border-radius: 1ch;
            border: 0px;
            margin-top: 10px;
            margin-right: 10px;
        }
        .container .part2 button:hover{
            color: greenyellow;
        }
        .container .part2 .result{
            margin-top: 20px;
            margin-left: 20px;
        }
        .container .part2 .result pre{
            font-size: large;
            font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="navbar">
            <a href="/">首页</a>
            <a href="/all_questions">题库</a>
            <a href="#">竞赛</a>
            <a href="#">讨论</a>
            <a href="#">求职</a>
            <a class="Register"href="#">登录</a>
            <a class="login"href="#">注册</a>
        </div>
        <!--左右呈现,题目描述和预设代码-->
        <div class="part1">
            <div class="left_desc">
                <h4><span id="number">{{number}}</span>.{{title}}.{{star}}</h4>
                <pre>{{desc}}</pre>
            </div>
            <div class="right_code">
                <pre id="code" class="ace_editor"><textarea class="ace_text-input">{{pre_code}}</textarea></pre>
            </div>
        </div>
        <!-- 提交并且得到结果并显示 -->
        <div class="part2">
            <div class="result"></div>
            <button class="btn-submit" onclick="submit()">提交代码</button>
        </div>
    </div>



    <script>
        //初始化对象 
        editor = ace.edit("code");
        //设置⻛格和语⾔（更多⻛格和语⾔，请到github上相应⽬录查看） 
        // 主题⼤全：http://www.manongjc.com/detail/25-cfpdrwkkivkikmk.html 
        editor.setTheme("ace/theme/monokai");
        editor.session.setMode("ace/mode/c_cpp");
        // 字体⼤⼩ 
        editor.setFontSize(16);
        // 设置默认制表符的⼤⼩: 
        editor.getSession().setTabSize(4);
        // 设置只读（true时只读，⽤于展⽰代码） 
        editor.setReadOnly(false);
        // 启⽤提⽰菜单
        ace.require("ace/ext/language_tools");

        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });

        function submit()
        {
            // alert("嘿嘿!");
            // 1.收集当前页面的有关数据,1.题号 2.代码
            // 哈哈!打印在控制台上 console.log("哈哈!");
            var code = editor.getSession().getValue();
            console.log(code);
            var number = $(".container .part1 .left_desc h4 #number").text();
            console.log(number);
            var judge_url = "/judge/" + number;
            console.log(judge_url);
            //2.构建json,并通过ajax向后台发起基于http的json请求
            $.ajax({
                method: 'Post',  //后端发起的请求方式
                url: judge_url,  //向后端指定的url发起请求
                dataType: 'json',//告知server,我需要什么格式
                contentType:"application/json;charset=utf-8",//告知server,我给你的格式
                data:JSON.stringify({//我们给对方的数据
                    "code":code,
                    "input":""
                }),
                success:function(data)
                {
                    //成功得到结果
                    // consloe.log(data);
                    show_result(data);
                }
            });
            //3.得到结果,解析并显示到result中
            function show_result(data)
            {
                //将错误码和原因拿到
                // console.log(data.status);
                // console.log(data.reason);
                //拿到result结果标签
                var result_div = $(".container .part2 .result");
                //清空上一次的运行结果
                result_div.empty();
                //首先拿到结果的状态码和原因结果
                var _status = data.status;
                var _reason = data.reason;
                var reason_div = $("<p>",{
                    text:_reason
                });
                reason_div.appendTo(result_div);
                if(_status == 0)
                {
                    //编译运行成功
                    var _stdout = data.stdout;
                    var _stderr = data.stderr;

                    var stdout_label = $("<pre>",{
                        text:_stdout
                    });
                    var stderr_label = $("<pre>",{
                        text:_stderr
                    });
                    stdout_label.appendTo(result_div);
                    stderr_label.appendTo(result_div);
                }
                else
                {
                    //编译运行出错,do nothing
                }
            }
        }
    </script>
</body>
</html>